<?php

//require_once '../modules/date_manager.class.php';
//require_once 'modules/date/date_manager.class.php';

class BaseModel
{
    //protected $_constantStatusList;  // by getStatusList() cache
    //protected $_localeManager;

    function __construct()
    {
        $this->resetValue();
    }

    /**
     *  object field value to array
     *  @return Array()
     */
    public function toArray()
    {
        $data = Array();
        foreach( $this->getTableFields() as $field => $method ) {
            $data[$field] = $this->$method();
        }
        return $data;
    }

    public function __sleep()
    {
        unset($this->_constantStatusList);
        unset($this->_localeManager);
    }

    public function resetValue()
    {
        $this->__sleep();
    }

    public function getModelName()
    {
        return get_class($this);
    }

    /*
        與語系結合, 列出相關資訊
    */
    /*
    public function pr( $locale ) {
        if( !isset($this->_localeManager) ) {
            $this->_localeManager = new LocaleManager();
            $this->_localeManager->loadLocale();
        }
        return $this->_localeManager->prm( $this->getModelName() , $locale );
    }
    */
    /*
        Translation sample:
                foreach( $user->getStatusList('authType') as $name => $id ) {
                    echo $user->trByStatus('authType',$id);
                    echo $user->trByName($name ;
                }
    */
    /**
     *  Translation 翻譯各種 status 的語系
     *  @param  string  $prefix - 辨示文字開頭, 不需要加 _ 符號
     *  @param  int     $id     - status id
     *  @return string
     *  @access public
     */
    public function trByStatus( $prefix , $id )
    {
        if( !isset($this->_localeManager) ) {
            $this->_localeManager = new LocaleManager();
            $this->_localeManager->loadLocale();
        }

        $prefix = strtolower($prefix);
        $list = $this->getStatusList($prefix);
        if(!$list) {
            throw new Exception("參數錯誤, status 不存在 - trByStatus( '{$prefix}', '{$id}' ) ");
        }

        foreach( $list as $name => $val ) {
            if( $id==$val ) {
                return $this->_localeManager->trm( $this->getModelName() , $name );
            }
        }

    }
    /**
     *  Translation 翻譯各種 status 的語系
     *  @param  string  $name - model locale name
     *  @return string
     *  @access public
     */
    public function trByName( $name )
    {
        if( !isset($this->_localeManager) ) {
            $this->_localeManager = new LocaleManager();
            $this->_localeManager->loadLocale();
        }
        return $this->_localeManager->trm( $this->getModelName() , $name );
    }



    /**
     *  get status list 取得該 model 所有的狀態
     *  @param  string  $prefix           - 辨示文字開頭, 不需要加 _ 符號
     *  @param  bool    $includeAllStatus - 是否包含取得 ******_all 的 status
     *  @return Array
     *  @access public
     */
    public function getStatusList( $prefix, $includeAllStatus=false )
    {
        if( !isset($this->_constantStatusList) ) {
            $reflection = new ReflectionObject($this);
            $this->_constantStatusList = $reflection->getConstants();
        }
        $constants = $this->_constantStatusList;
        $prefix = strtolower($prefix);

        $statusList = Array();
        foreach( $constants as $constant => $value ) {
            $constant = strtolower($constant);
            if( substr($constant, 0, strlen($prefix))==$prefix ) {
                if( !$includeAllStatus  &&  $constant == $prefix.'_all' ) {
                    // can't include ******_all
                }
                else {
                    $statusList[$constant] = $value;
                }
            }
        }

        /*
            echo '<pre style="background-color:#def;color:#000;text-align:left;font-size:10px;font-family:dina,GulimChe;">';
            print_r($constants);
            print_r($statusList);
            echo "</pre>\n";
        */
        asort( $statusList );
        return $statusList;
    }

    /*
        basic validate
    */
    public function validate()
    {
        $validateInfo = Array();
        $tableDefinition = $this->getTableDefinition();
        $tableFields     = $this->getTableFields();
        foreach( $tableFields as $field => $method ) {
            if( !isset($tableDefinition[$field]) ) {
                continue;
            }
            if( 'integer'==$tableDefinition[$field]['type'] )
            {
                $value = $this->$method();
                if(!is_int($value)) {
                    $validateInfo[$field] = Array('result'=>false);
                }
            }
            
            /*
            if( $tableDefinition[$field]['type']=='timestamp' )
            {
                // validate "1970-01-01 00:00:00"
            }
            */

        }
        return $validateInfo;
    }

    //================================================================================
    // about table field
    //================================================================================

    /**
     *  id field
     */
    public function setId($int) 
    {
        $this->_id = (int) $int;
    }
    public function getId() 
    {
        return $this->_id;
    }

    /**
     *  set createDate 
     *  @param int , date int
     */
    public function setCreateDate($int) 
    {
        $this->_createDate = (int) $int;
    }
    /**
     *  get createDate 
     *  @return int;
     */
    public function getCreateDate() 
    {
        if( !$this->_createDate ) {
            $this->_createDate = time();
        }
        return $this->_createDate;
    }
    /**
     *  get createDate 
     *  @param  format , date format string
     *  @return string
     */
    public function getCreateDateByFormat( $format="Y-m-d" )
    {
        return date($format,$this->getCreateDate());
    }
    /**
     *  convert object-value to database 
     *  @return string
     */
    public function getCreateDateByDb()
    {
        // "timestamp"
        return date("Y-m-d H:i:s",$this->getCreateDate());
    }



    /**
     *  set updateDate 
     *  @param int , date int
     */
    public function setUpdateDate($int) 
    { 
        $this->_updateDate = (int) $int;  
    }
    /**
     *  get updateDate 
     *  @return int;
     */
    public function getUpdateDate() 
    {
        if( !$this->_updateDate ) {
            $this->_updateDate = time();
        }
        return $this->_updateDate;
    }
    /**
     *  get updateDate 
     *  @param  format , date format string
     *  @return string
     */
    public function getUpdateDateByFormat( $format="Y-m-d" )
    {
        return date($format,$this->getUpdateDate());
    }
    /**
     *  convert object-value to database 
     *  @return string
     */
    public function getUpdateDateByDb()
    {
        // "timestamp"
        return date("Y-m-d H:i:s",$this->getUpdateDate());
    }


    /**
     *  properties field
     */
    public function setProperties($properties) 
    {
        if( is_array($properties) ) {
            $this->_properties = $properties;
        }
        else {
            $this->_properties = Array();
        }
    }
    public function getProperties() 
    {
        return $this->_properties;
    }
    public function setProperty($key,$value) 
    {
        $this->_properties[$key] = $value;
    }
    public function getProperty($key) 
    {
        return $this->_properties[$key];
    }

}

